/*
 * @Author: xmily
 * @Date: 2021-01-01 17:30:18
 * @LastEditors: xmily
 * @LastEditTime: 2022-02-14 21:54:24
 * @FilePath: \glxxapi\routes\wechat\interest.js
 * @Description: 
 * 
 * Copyright (c) 2022 by xmily/mxthink.com.cn, All Rights Reserved. 
 */
const router = require('koa-router')()
const axios = require('axios')
const jwt = require('jsonwebtoken')
const interest = require('../../models/interest/index');
const base = require('../../models/Fjxx');
const sitecfg = require('../../cfg/siteconfig.js')
var wxuserinfo = null;

router.prefix('/interest')

//拦截所有请求，如果有token则将用户信息注入到请求中
router.use(async (ctx, next) => {
    console.log('拦截的访问:' + ctx.request.href)
    if (!ctx.header.authorization) {
        throw new Error('关键数据链接失效或者是非法的！')
    }
    wxuserinfo = {}
    try {
        wxuserinfo = await jwt.verify(ctx.header.authorization, sitecfg.tokenKey);
    } catch (err) {
        throw new Error('关键数据链接失效或者是非法的！')
    }
    await next();
})

router.post('/save/', async (ctx, next) => {
    let interestinfo = ctx.request.body.interestinfo;
    let res = null;
    try {
        if (interestinfo._id) {
            res = await interest.Update(interestinfo)
        } else {
            res = await interest.Save(interestinfo)
        }
        ctx.body = { 'error': false, result: res }
    } catch (err) {
        throw new Error("保存时出错:[" + err + "]")
    }
})
router.post('/get/', async (ctx, next) => {
    let pid = ctx.request.body.pid;
    try {
        let res = await interest.GetByQuery({ pid: pid })
        ctx.body = { "error": false, result: res }
    } catch (err) {
        throw new Error("保存时出错:[" + err + "]");
    }
})
module.exports = router